*** ONLINE APPENDIX D: ROBUSTNESS CHECKS ***

clear all
cap log close

global path ""
use "$path/Main.dta", clear

// Table D1: Statistics, by Dynastic Linkage //
estpost tabstat log_pc_x_totexp povincidence log_DMSP_AVP_MEAN popn land_area pct_urban pctvote party_pres term experience sex, by(position_rel) not statistics(mean sd) columns(statistics) listwise

// Figure D1: Balance of Predetermined Variables - Dynasty (H1) //
* Subfigures (a) to (f) in order
foreach x in dynasty_inc_1_lag dynasty_inc1_G_lag dynasty_inc1_VG_lag dynasty_inc1_M_lag dynasty_inc1_VM_lag dynasty_inc1_CONG_lag {
local vartitle : variable label `x' 

preserve
drop if margin_rel == .

lpoly `x' margin_rel if margin_rel > 0, gen(marpos polpos) ci se(sepos) nograph degree(2)
lpoly `x' margin_rel if margin_rel < 0, gen(marneg polneg) ci se(seneg) nograph degree(2)

foreach v in pos neg {
gen ul`v' = pol`v' + 1.95*se`v'
gen ll`v' = pol`v' - 1.95*se`v'
}

sort marpos
twoway (rarea ulpos llpos marpos, fcolor(gs14) lwidth(none)) (rarea ulneg llneg marneg, fcolor(gs14) lwidth(none)) ///
(scatter `x'_mv_20 mv_20, msymbol(circle) mlcolor(black) mfcolor(none)) (line polpos marpos, lpattern(solid)  lcolor(black)) (line polneg marneg, lpattern(solid)  lcolor(black)), ///
xline(0, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(off) ytitle("`vartitle'") xtitle("Margin") xscale(titlegap(2)) xlabel(-1(0.1)1) 

restore

}

// Figure D2: Balance of Predetermined Variables (H1) //
* Subfigures (a) to (j) in order
foreach x in log_pc_x_totexp_lag povincidence_lag log_DMSP_AVP_MEAN_lag term_lag num_cand_lag party_pres_primary_lag pctvote_lag pct_urban_lag land_area_lag popn_lag {
local vartitle : variable label `x' 

preserve
drop if margin_rel == .

lpoly `x' margin_rel if margin_rel > 0, gen(marpos polpos) ci se(sepos) nograph
lpoly `x' margin_rel if margin_rel < 0, gen(marneg polneg) ci se(seneg) nograph

foreach v in pos neg {
gen ul`v' = pol`v' + 1.95*se`v'
gen ll`v' = pol`v' - 1.95*se`v'
}

sort marpos
twoway (rarea ulpos llpos marpos, fcolor(gs14) lwidth(none)) (rarea ulneg llneg marneg, fcolor(gs14) lwidth(none)) ///
(scatter `x'_mv_20 mv_20, msymbol(circle) mlcolor(black) mfcolor(none)) (line polpos marpos, lpattern(solid) lcolor(black)) (line polneg marneg, lpattern(solid) lcolor(black)), ///
xline(0, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(off) ytitle("`vartitle'") xtitle("Margin") xscale(titlegap(2)) xlabel(-1(0.1)1) 

restore

}

// Figure D3: Forcing Variable Histogram, By Dynastic Linkage (H1) //
gen position_rel2 = "Vice Mayor (N=896)" if position_rel == "VICE-MAYOR"
replace position_rel2 = "Mayor (N=501)" if position_rel == "MAYOR"
replace position_rel2 = "Vice Governor (N=101)" if position_rel == "VICE-GOVERNOR"
replace position_rel2 = "Governor (N=144)" if position_rel == "GOVERNOR"
replace position_rel2 = "Congressperson (N=320)" if position_rel == "CONGRESSMAN"

hist margin_rel, by(position_rel2, total note("") legend(off) graphregion(color(white)) plotregion(color(white))) ///
ylabel(, nogrid) bcolor(gs14) barwidth(0.06) xtitle("Margin") ytitle("Density") 

// Figure D5: McCrary Density Test (H3) //
* Subfigure (a)
rddensity margin_rel if log_DMSP_AVP_MEAN != ., plot graph_options(ytitle("Density") xtitle("Margin") legend(off) graphregion(color(white)))

* Subfigure (b)
rddensity margin_rel if povincidence != ., plot graph_options(ytitle("Density") xtitle("Margin") legend(off) graphregion(color(white)))

// Figure D4: Specification Chart by Control Variable - RD (H1) //	
cap program drop specchart
program specchart
syntax varlist, [replace] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			save "estimates.dta",replace
	}	
	else{
		* load dataset
		use "estimates.dta",clear
	}
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
		save "estimates.dta",replace
	* restore dataset
	use `temp',clear
end

capture reg log_pc_x_totexp win   
specchart  win,spec(FE_no FCN_no) replace

rdbwselect_2014 log_pc_x_totexp margin_rel, bwselect(CCT)
local bw_CCT = e(h_CCT)

rdbwselect_2014 log_pc_x_totexp margin_rel, bwselect(IK)
local bw_IK = e(h_IK)

local bw_FULL = 1

local FCN_no ""
local FCN_linear "mv_1 dmv_1"
local FCN_quadratic "mv_1 dmv_1 mv_2 dmv_2"

local CON_1 "experience"
local CON_2 "sex"
local CON_3 "dynasty_inc_2"
local CON_4 "num_cand_rel"
local CON_5 "log_DMSP_AVP_MEAN_lag"
local CON_6 "pct_urban"

foreach r in no linear quadratic {
foreach b in CCT IK FULL {
local bw`b' = round(`bw_`b'', 0.01)

xtset idcode

foreach c of num 1/6 {
capture reg log_pc_x_totexp win `FCN_`r'' `CON_`c'' if position == "MAYOR" & abs(margin_rel) <= `bw_`b'', robust cluster(idcode_prov)
specchart  win,spec(CON_`c' FCN_`r' BW_`b') 

capture reg log_pc_x_totexp win `FCN_`r'' `CON_`c'' i.year if position == "MAYOR" & abs(margin_rel) <= `bw_`b'', robust cluster(idcode_prov)
specchart  win,spec(CON_`c' FE_year FCN_`r' BW_`b') 

capture xi: xtreg log_pc_x_totexp win `FCN_`r'' `CON_`c'' if position == "MAYOR" & abs(margin_rel) <= `bw_`b'', fe robust cluster(idcode_prov)
specchart  win,spec(CON_`c' FE_mun FCN_`r' BW_`b') 

capture xi: xtreg log_pc_x_totexp win `FCN_`r'' `CON_`c'' i.year if position == "MAYOR" & abs(margin_rel) <= `bw_`b'', fe robust cluster(idcode_prov)
specchart  win,spec(CON_`c' FE_year FE_mun FCN_`r' BW_`b')
}

}
}

use "estimates.dta",clear
drop if _n == 1
replace spec_id = spec_id - 1

duplicates drop CON_1 CON_2 CON_3 CON_4 CON_5 CON_6 FE_year FE_mun FCN_no FCN_linear FCN_quadratic BW_FULL BW_CCT BW_IK, force

drop if (BW_IK == 1 | BW_CCT == 1) & (FCN_linear == 1 | FCN_quadratic == 1)
drop if (FE_mun == 1 & FE_year == .) | (FE_mun == . & FE_year == 1)
drop if CON_1 == 1 & CON_2 == 1 & CON_3 == 1 & CON_4 == 1 & CON_5 == 1 & CON_6 == 1

gsort -CON_6 -CON_5 -CON_4 -CON_3 -CON_2 -CON_1 ///
	-FE_mun -FE_year ///
	-BW_IK -BW_CCT -BW_FULL ///
	-FCN_quadratic -FCN_linear -FCN_no, mfirst

gen rank=_n

	local scoff=" "
	local scon=" "
	local ind=-0.31

	foreach var in CON_1 CON_2 CON_3 CON_4 CON_5 CON_6 FE_year FE_mun BW_FULL BW_CCT BW_IK FCN_no FCN_linear FCN_quadratic {
	   cap gen i_`var'=`ind'
	   local ind=`ind'-0.05
	   
	   local scoff="`scoff' (scatter i_`var' rank,msize(vsmall) mcolor(gs10))" 
	   local scon="`scon' (scatter i_`var' rank if `var'==1,msize(vsmall) mcolor(black))"
	   
	   if "`var'" == "CON_6" | "`var'" == "FE_mun" | "`var'" == "BW_IK" {
	   local ind=`ind'-0.1
	   }
	}

gen zero = 0

tw (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) lwidth(none)) /// 90% CI
   (scatter beta rank, mcolor(black) msymbol(d) msize(*0.2)) ///  point estimates
   (line zero rank, lcolor(red) lpattern(shortdash)) /// zero line
   `scoff' `scon' /// indicators for spec
   , legend(order(3 "Point Estimate" 1 "95% CI" 2 "90% CI") region(lcolor(white)) ///
	pos(6) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") title("Dependent Variable: Spending", size(medsmall)) ///
   yscale(noline) xscale(noline) ylab(-0.1(0.1)0.3,noticks nogrid angle(horizontal) labsize(vsmall)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white))
    
gr_edit .yaxis1.add_ticks -0.25 `"Control Variable"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -0.3 `"Tenure"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.35 `"Gender"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.4 `"Dynastic Past"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.45 `"No. of Candidates"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.5 `"Night Lights"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.55 `"Urban"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -0.65 `"Fixed Effects"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -0.7 `"Year"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.75 `"Municipality"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -0.85 `"Bandwidth"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -0.9 `"Full"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.95 `"CCT"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.0 `"IK"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -1.1 `"Function"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -1.15 `"None"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.2 `"Linear"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.25 `"Quadratic"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks 0.4 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
